{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.2.3\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "print(pd.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-11   -1.556662\n",
      "2019-01-22    0.127451\n",
      "2019-01-13      str-AA\n",
      "2019-01-14    -1.37775\n",
      "dtype: object\n",
      "2019-01-11    0\n",
      "2019-01-22    1\n",
      "2019-01-13    2\n",
      "2019-01-14    3\n",
      "dtype: int64\n",
      "2019-01-11    5.0\n",
      "2019-01-22    5.0\n",
      "2019-01-13    5.0\n",
      "2019-01-14    5.0\n",
      "dtype: float64\n",
      "2019-01-11    0.0\n",
      "2019-01-12    1.0\n",
      "2019-01-13    2.0\n",
      "2019-01-14    3.0\n",
      "dtype: float64\n",
      "2019-01-13    NaN\n",
      "2019-01-14    NaN\n",
      "2019-01-11    0.0\n",
      "2019-01-12    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 列表\n",
    "s_list = pd.Series([-1.55666192,0.127451231,\"str-AA\",-1.37775038],\n",
    "                   index=['2019-01-11','2019-01-22','2019-01-13','2019-01-14'])\n",
    "print(s_list)\n",
    "# ndarray\n",
    "s_ndarray = pd.Series(np.arange(4), index=['2019-01-11','2019-01-22','2019-01-13','2019-01-14'] )\n",
    "print(s_ndarray)\n",
    "# 标量\n",
    "s_scalar = pd.Series(5., index=['2019-01-11','2019-01-22','2019-01-13','2019-01-14'])\n",
    "print(s_scalar)\n",
    "\n",
    "# 字典\n",
    "s_dict = pd.Series({'2019-01-11' : 0.,'2019-01-12' : 1.,'2019-01-13' : 2., '2019-01-14' : 3.})\n",
    "print(s_dict)\n",
    "\n",
    "#元素量少于索引，缺失为NaN\n",
    "s_dict = pd.Series({'2019-01-11' : 0., '2019-01-12' :1.,}, index = {'2019-01-11', '2019-01-12', '2019-01-13', '2019-01-14'})\n",
    "print(s_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-11    10.23\n",
      "2019-01-12    11.24\n",
      "2019-01-13    12.25\n",
      "2019-01-14    13.26\n",
      "dtype: float64\n",
      "[10.23 11.24 12.25 13.26]\n",
      "Index(['2019-01-11', '2019-01-12', '2019-01-13', '2019-01-14'], dtype='object')\n",
      "10.23\n",
      "2019-01-11    10.23\n",
      "2019-01-13    12.25\n",
      "dtype: float64\n",
      "2019-01-11    10.23\n",
      "2019-01-12    11.24\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "series_access = pd.Series([10.23, 11.24, 12.25, 13.26], index=['2019-01-11','2019-01-12','2019-01-13','2019-01-14'])\n",
    "\n",
    "print(series_access)\n",
    "\n",
    "# 访问全部元素数值\n",
    "print(series_access.values)\n",
    "# 访问全部索引值\n",
    "print(series_access.index)\n",
    "# 访问Series2019-01-11索引的元素值\n",
    "print(series_access['2019-01-11'])\n",
    "# 访问2019-01-11和2019-01-13索引的元素值\n",
    "print(series_access[['2019-01-11','2019-01-13']])\n",
    "# 访问前两个数据\n",
    "print(series_access[:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            Close  Open\n",
      "2019-01-11    1.0   1.0\n",
      "2019-01-12    2.0   2.0\n",
      "2019-01-13    3.0   3.0\n",
      "2019-01-14    5.0   4.0\n",
      "            Close  Open  Low  High\n",
      "2019-01-11    1.0   2.0  3.0   5.0\n",
      "2019-01-12    1.0   2.0  3.0   4.0\n",
      "[(0, 0., b'') (0, 0., b'')]\n",
      "            Close  Open      Low\n",
      "2019-01-11      1   2.0  b'11.2'\n",
      "2019-01-12      2   3.0  b'12.3'\n",
      "            Close  Open\n",
      "2019-01-11    1.0   1.0\n",
      "2019-01-12    2.0   2.0\n",
      "2019-01-13    3.0   3.0\n",
      "2019-01-14    NaN   4.0\n",
      "            Close  Open  High\n",
      "2019-01-11      1     2   NaN\n",
      "2019-01-12      5    10  20.0\n",
      "Index(['2019-01-11', '2019-01-12'], dtype='object')\n",
      "Index(['Close', 'Open', 'High'], dtype='object')\n",
      "[Index(['2019-01-11', '2019-01-12'], dtype='object'), Index(['Close', 'Open', 'High'], dtype='object')]\n",
      "[[ 1.  2. nan]\n",
      " [ 5. 10. 20.]]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 以列表组成的字典形式创建\n",
    "df_list_dict = pd.DataFrame({'Close': [1., 2., 3., 5], 'Open': [1., 2., 3., 4.]}, index=['2019-01-11', '2019-01-12', '2019-01-13', '2019-01-14'])\n",
    "print(df_list_dict) # 创建4行2列的表格\n",
    "\n",
    "df_list_list = pd.DataFrame([[1.,2.,3., 5],[1.,2.,3.,4.]],index=['2019-01-11', '2019-01-12'],columns=['Close','Open','Low','High'])\n",
    "print(df_list_list)\n",
    "\n",
    "ndarray_data = np.zeros((2), dtype=[('Close', 'i4'),('Open', 'f4'),('Low', 'S10')]) #整数、浮点和字符串\n",
    "print(ndarray_data)\n",
    "\n",
    "ndarray_data[:] = [(1,2.,'11.2'), (2,3.,\"12.3\")]\n",
    "# 使用默认的定列索引，也可指定列索引columns，这样最终按指定的顺序进行排列\n",
    "df_ndarray = pd.DataFrame(data=ndarray_data, index=['2019-01-11', '2019-01-12'])\n",
    "print(df_ndarray)\n",
    "# 以Series组成的字典形式创建DataFrame,会自动加上缺失数据，Series的索引会被合并成DataFrame的行索引\n",
    "series_data = {'Close' : pd.Series([1., 2., 3.], index=['2019-01-11', '2019-01-12', '2019-01-13']), 'Open' : pd.Series([1., 2., 3., 4.], index=['2019-01-11', '2019-01-12', '2019-01-13', '2019-01-14'])}\n",
    "df_series = pd.DataFrame(series_data)\n",
    "print(df_series)\n",
    "#如果不指定行索引 index DataFrame会自动加上行索引\n",
    "df_dict_list = pd.DataFrame([{'Close' : 1, 'Open' :2}, {'Close' : 5, 'Open' : 10, 'High':20}] , index=['2019-01-11', '2019-01-12'])\n",
    "print(df_dict_list)\n",
    "\n",
    "# 行索引\n",
    "print(df_dict_list.index)\n",
    "# 列索引\n",
    "print(df_dict_list.columns)\n",
    "# 全部行和列\n",
    "print(df_dict_list.axes)\n",
    "\n",
    "# 全部元素\n",
    "print(df_dict_list.values)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-11     2\n",
      "2019-01-12    10\n",
      "Name: Open, dtype: int64\n",
      "2019-01-11     2\n",
      "2019-01-12    10\n",
      "Name: Open, dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n",
      "            Close  Open  High\n",
      "2019-01-11      1     2   NaN\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "            Close  Open\n",
      "2019-01-11      1     2\n",
      "            Close  Open\n",
      "2019-01-11      1     2\n",
      "2019-01-12      5    10\n",
      "Close    1.0\n",
      "Open     2.0\n",
      "High     NaN\n",
      "Name: 2019-01-11, dtype: float64\n",
      "            Close\n",
      "2019-01-11      1\n",
      "2019-01-12      5\n",
      "            Close  Open  High\n",
      "2019-01-11      1     2   NaN\n",
      "2019-01-12      5    10  20.0\n",
      "            Close  High\n",
      "2019-01-11      1   NaN\n",
      "2019-01-12      5  20.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_dict_list = pd.DataFrame([{'Close' : 1, 'Open' :2}, {'Close' : 5, 'Open' : 10, 'High':20}] , index=['2019-01-11', '2019-01-12'])\n",
    "# 访问某列元素\n",
    "print(df_dict_list['Open'])\n",
    "print(df_dict_list.Open)\n",
    "# 查看列类型\n",
    "print(type(df_dict_list['Open']))\n",
    "# 第一行元素\n",
    "print(df_dict_list[0:1])\n",
    "# 查看行类型\n",
    "print(type(df_dict_list[0:1]))\n",
    "# 选取元素\n",
    "print(df_dict_list.loc[['2019-01-11',],['Close','Open']])\n",
    "print(df_dict_list.loc[:,['Close','Open']])\n",
    "print(df_dict_list.loc['2019-01-11'])\n",
    "# 前两行第一列\n",
    "print(df_dict_list.iloc[0:2,0:1])\n",
    "# 前两行所有列\n",
    "print(df_dict_list.iloc[0:2])\n",
    "\n",
    "print(df_dict_list.iloc[[0,1],[0,2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['2019-01-11', '2019-01-12'], dtype='object')\n",
      "            Open\n",
      "2019-01-11     2\n",
      "2019-01-12    10\n",
      "[1]\n",
      "1\n",
      "            Open\n",
      "2019-01-11     2\n",
      "2019-01-12    10\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_dict_list = pd.DataFrame([{'Close' : 1, 'Open' :2}, {'Close' : 5, 'Open' : 10, 'High':20}] , index=['2019-01-11', '2019-01-12'])\n",
    "print(df_dict_list.index[[0, 1]])\n",
    "print(df_dict_list.loc[df_dict_list.index[[0,1]], ['Open']])\n",
    "\n",
    "print(df_dict_list.columns.get_indexer(['Open']))\n",
    "print(df_dict_list.columns.get_loc('Open'))\n",
    "print(df_dict_list.iloc[[0, 1], df_dict_list.columns.get_indexer(['Open'])])\n",
    "\n",
    "print(df_dict_list.index.get_loc('2019-01-12'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            Close   Open\n",
      "2019-01-11  10.51  12.31\n",
      "2019-01-12  10.52  12.32\n",
      "2019-01-13  10.53  12.33\n",
      "2019-01-14  10.54  12.34\n",
      "2019-01-11    False\n",
      "2019-01-12    False\n",
      "2019-01-13     True\n",
      "2019-01-14     True\n",
      "Name: Open, dtype: bool\n",
      "            Close   Open\n",
      "2019-01-13  10.53  12.33\n",
      "2019-01-14  10.54  12.34\n",
      "2019-01-13    10.53\n",
      "2019-01-14    10.54\n",
      "Name: Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "series_data = {'Close' : pd.Series([10.51, 10.52, 10.53, 10.54], index=['2019-01-11', '2019-01-12', '2019-01-13', '2019-01-14']), \n",
    "               'Open' : pd.Series([12.31, 12.32, 12.33, 12.34], index=['2019-01-11', '2019-01-12', '2019-01-13', '2019-01-14'])}\n",
    "df_access = pd.DataFrame(series_data)\n",
    "print(df_access)\n",
    "# open列大于平均值\n",
    "print(df_access.Open > df_access.Open.mean())\n",
    "print(df_access[df_access.Open > df_access.Open.mean()])\n",
    "print(df_access.loc[df_access.Open > df_access.Open.mean(), 'Close'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
